import 'package:cloudapp/common/common.dart';
import 'package:cloudapp/common/def.dart';
import 'package:flutter/material.dart';
import 'package:progress_dialog/progress_dialog.dart';

abstract class BaseState<T extends StatefulWidget> extends State<T> {
  ProgressDialog _pr;

  @override
  void initState() {
    super.initState();
    _pr = ProgressDialog(
      context,
      isDismissible: true,
      showLogs: false,
    );
  }

  Future<bool> showLoading() async {
    return await _pr.show(context: context);
  }

  Future<bool> hideLoading() async {
    return await _pr.hide();
  }

  Future<X> loading<X>(FutureFunc<X> cb) async {
    await _pr.show(context: context);

    try {
      return await withExc(cb);
    } finally {
      await _pr.hide();
    }
  }

  Future<T> withExc<T>(FutureFunc<T> cb) async {
    return withException(context, cb);
  }
}
